package com.wjc.lib


/***
 * 委托
 */
object ByClass {

    @JvmStatic
    fun main(args: Array<String>) {
        val b = BaseImpl(10)
        Derived(b).print01()
//        Derived(b).print02()
        print(Derived(b).message)
    }

    // 所有公有成员都委托给指定对象(BaseImpl)来实现一个接口 Base
    class Derived(b: Base) : Base by b {
        override val message: String = "-Derived message"
        override fun print02() { // 编译器会使用 override 覆盖的实现而不是委托对象中的
            print("Derived ---  $message")
        }
    }

    class BaseImpl(val x: Int) : Base {
        override val message = "BaseImpl x=$x"

        override fun print01() {
            println(message)
        }

        override fun print02() {
            print(x)
        }
    }


    interface Base {
        val message: String
        fun print01()
        fun print02()

    }
}